package com.xingdata.jjxc.record;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xingdata.jjxc.R;
import com.xingdata.jjxc.activity.NaviSerchActivity;

public class SearchRecordAdapter extends BaseAdapter {
	private Context mContext;
	private ArrayList<SearchRecordEntity> searchRecordEntityArrayList;// 所有的Item
	private List<SearchRecordEntity> searchRecordEntityList;// 过滤后的item
	private final Object mLock = new Object();
	private int mMaxMatch = 10;// 最多显示多少个选项,负数表示全部
	private OnClickListener mOnClickListener;

	public SearchRecordAdapter(Context context, int maxMatch, OnClickListener onClickListener) {
		this.mContext = context;
		this.mMaxMatch = maxMatch;
		this.mOnClickListener = onClickListener;
		initSearchHistory();
		searchRecordEntityList = searchRecordEntityArrayList;
	}

	@Override
	public int getCount() {
		Log.i("cyl", "getCount");
		return null == searchRecordEntityList ? 0 : searchRecordEntityList.size();
	}

	@Override
	public Object getItem(int position) {
		return null == searchRecordEntityList ? 0 : searchRecordEntityList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		AutoHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.auto_seach_list_item, parent, false);
			holder = new AutoHolder();
			holder.search_placeName = (TextView) convertView.findViewById(R.id.search_placeName);
			holder.search_placeAddress = (TextView) convertView.findViewById(R.id.search_placeAddress);
			
//			holder.addButton = (ImageView) convertView.findViewById(R.id.auto_add);
//			holder.autoImage = (ImageView) convertView.findViewById(R.id.auto_image);
			convertView.setTag(holder);
		} else {
			holder = (AutoHolder) convertView.getTag();
		}

		SearchRecordEntity data = searchRecordEntityList.get(position);
		holder.search_placeName.setText(data.getPlaceName());
		holder.search_placeAddress.setText(data.getId());
		holder.search_placeAddress.setVisibility(View.GONE);
		
//		holder.addButton.setTag(data);
//		holder.addButton.setOnClickListener(mOnClickListener);
		return convertView;
	}

	/**
	 * 读取历史搜索记录
	 */
	public void initSearchHistory() {
		SharedPreferences sp = mContext.getSharedPreferences(NaviSerchActivity.SEARCH_HISTORY, 0);
		String longhistory = sp.getString(NaviSerchActivity.SEARCH_HISTORY, "");
		String[] hisArrays = longhistory.split(",");
		searchRecordEntityArrayList = new ArrayList<SearchRecordEntity>();
		if (hisArrays.length == 1) {
			return;
		}
		for (int i = 0; i < hisArrays.length; i++) {
			searchRecordEntityArrayList.add(new SearchRecordEntity().setPlaceName(hisArrays[i]));
		}
	}

	/**
	 * 匹配过滤搜索内容
	 * 
	 * @param prefix
	 *            输入框中输入的内容
	 */
	public void performFiltering(CharSequence prefix) {
		if (prefix == null || prefix.length() == 0) {// 搜索框内容为空的时候显示所有历史记录
			synchronized (mLock) {
				searchRecordEntityList = searchRecordEntityArrayList;
			}
		} else {
			String prefixString = prefix.toString().toLowerCase();
			int count = searchRecordEntityArrayList.size();
			ArrayList<SearchRecordEntity> newValues = new ArrayList<SearchRecordEntity>(count);
			for (int i = 0; i < count; i++) {
				final String value = searchRecordEntityArrayList.get(i).getPlaceName();
				final String valueText = value.toLowerCase();
				if (valueText.contains(prefixString)) {

				}
				if (valueText.startsWith(prefixString)) {
					newValues.add(new SearchRecordEntity().setPlaceName(valueText));
				} else {
					final String[] words = valueText.split(" ");
					final int wordCount = words.length;
					for (int k = 0; k < wordCount; k++) {
						if (words[k].startsWith(prefixString)) {
							newValues.add(new SearchRecordEntity().setPlaceName(value));
							break;
						}
					}
				}
				if (mMaxMatch > 0) {
					if (newValues.size() > mMaxMatch - 1) {
						break;
					}
				}
			}
			searchRecordEntityList = newValues;
		}
		notifyDataSetChanged();
	}

	private class AutoHolder {
		TextView search_placeName;
		TextView search_placeAddress;
//		ImageView addButton;
//		ImageView autoImage;
	}
}
